"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具查询该节点是否注册
Create At   : 2023/4/20
Owner       : opentestcase019
Description :
    1.查询该节点是否注册，查看帮助信息
    2.查询该节点是否注册，不指定参数
    3.查询该节点是否注册，指定-i inst_id参数为有效值
    4.查询该节点是否注册，指定-i inst_id参数为无效值
    5.分别在主机和备机执行查询该节点是否注册，指定-D DSS_HOME参数为正常值
    6.查询该节点是否注册，指定错误的-D DSS_HOME参数
    7.查询该节点是否注册，指定权限不足的-D DSS_HOME参数
    8.在dss关闭的情况下执行查询该节点是否注册
    9.在集群停止的情况下执行
    10.DSS_HOME路径权限不足时，查询该节点是否注册，不指定-D DSS_HOME参数
Expect      :
    1.查看成功
    2.合理报错
    3.查询成功，结果显示正常
    4.合理报错
    5.查询成功，结果显示正常
    6.合理报错
    7.合理报错
    8.查询成功，结果显示正常
    9.查询成功，结果显示正常
    10.合理报错
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:查询该节点是否注册，查看帮助信息;expect:查看成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd inq_reg -h"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Usage:dsscmd inq_reg <-i inst_id> [-D DSS_HOME]\n'
                      '[raid command]check whether the node is registered\n'
                      '-i/--inst_id <inst_id>, <required>, the id of the '
                      'host need to reg\n-D/--DSS_HOME <DSS_HOME>, '
                      '[optional], the run path of dssserver, default value '
                      'is $DSS_HOME', execute_msg, "执行失败" + step1)

        step2 = "-----step2:查询该节点是否注册，不指定参数;expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd inq_reg;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('args [-i|--inst_id] needs input value.',
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:查询该节点是否注册，指定-i inst_id参数为有效值;" \
                "expect:查询成功，结果显示正常-----"
        self.logger.info(step3)
        cmd_list = ['-i 0', '-i 1', '-i 63',
                    '--inst_id 0', '--inst_id 1', '--inst_id 63']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd inq_reg {cmd};"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn('Succeed to inq reg host',
                          execute_msg, "执行失败" + step3)

        step4 = "-----step4:查询该节点是否注册，指定-i inst_id参数为无效值;expect:合理报错-----"
        self.logger.info(step4)
        cmd_list = ['-i 64', '-i -1', '-i zero', '-i %&@', '-i 1@']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd inq_reg {cmd};"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == cmd_list[0]:
                self.assertIn('The value of inst_id should be in [0, 64).',
                              execute_msg, "执行失败" + step4)
            else:
                self.assertIn('The value of inst_id is invalid.',
                              execute_msg, "执行失败" + step4)

        step5 = "-----step5:分别在主机和备机执行查询该节点是否注册，" \
                "指定-D DSS_HOME参数为正常值;expect:查询成功，结果显示正常-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd inq_reg -i 0 -D {macro.DSS_INSTANCE};" \
            f"dsscmd inq_reg -i 1 -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to inq reg host 0', execute_msg, "执行失败" + step5)
        self.assertIn('Succeed to inq reg host 1', execute_msg, "执行失败" + step5)

        step6 = "-----step6:查询该节点是否注册，指定错误的-D DSS_HOME参数;expect:合理报错-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd inq_reg -i 0 -D {macro.DSS_INSTANCE}.bak;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Failed to inq reg host 0.', execute_msg, "执行失败" + step6)

        step7 = "-----step7:查询该节点是否注册，指定权限不足的-D DSS_HOME参数;expect:合理报错-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.DSS_INSTANCE};" \
            f"dsscmd inq_reg -i 0 -D {macro.DSS_INSTANCE};" \
            f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Failed to inq reg host 0", execute_msg, "执行失败" + step7)

        step8 = "-----step8:在dss关闭的情况下执行查询该节点是否注册;" \
                "expect:查询成功，结果显示正常-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss;sleep 2;" \
            f"dsscmd inq_reg -i 0;" \
            f"dsscmd inq_reg -i 1 -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step8)
        self.assertEquals(2, execute_msg.count("Succeed to inq reg host"),
                          "执行失败" + step8)

        step9 = "-----step9:在集群停止的情况下执行;expect:查询成功，结果显示正常-----"
        self.logger.info(step9)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step9)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd inq_reg -i 0;" \
            f"dsscmd inq_reg -i 1 -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertEquals(2, execute_msg.count("Succeed to inq reg host"),
                          "执行失败" + step9)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')

        step10 = "-----step10:DSS_HOME路径权限不足时，查询该节点是否注册，" \
                 "不指定-D DSS_HOME参数;expect:合理报错-----"
        self.logger.info(step10)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.DSS_INSTANCE};" \
            f"dsscmd inq_reg -i 1;" \
            f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Failed to inq reg host 1", execute_msg, "执行失败" + step10)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
